//
//  HNSH_GroupListViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/6.
//

#import "HNSH_GroupListViewController.h"

#import "HNSH_GroupListCell.h"

#import "HNSH_ChatGroupViewController.h"

@interface HNSH_GroupListViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

@end

static NSString *HNSH_GroupListCellID = @"HNSH_GroupListCellID";

@implementation HNSH_GroupListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showNavTitle:@"群聊" showLeftItem:YES];
    
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_news"] title:@""];
    
    self.tableView.rowHeight = 64;
    [self.tableView registerClass:[HNSH_GroupListCell class] forCellReuseIdentifier:HNSH_GroupListCellID];
}

#pragma mark lazy

- (void)refreshData:(NSInteger)page {
    [HNSH_ChatNetServer requestRongCloudGroupListWithPage:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray * list = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_GroupInfoUserModel.class json:list];
            [self.listAry addObjectsFromArray:array];
            [self.tableView reloadData];
            [self endRefresh:array.count >= 10];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listAry.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_GroupListCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_GroupListCellID];
    cell.model = self.listAry[indexPath.row];

    return cell;
}
//8e6035c5cd8daf2185ce9d4158badba3
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_ChatGroupViewController *vc = [[HNSH_ChatGroupViewController alloc]init];
    HNSH_GroupInfoUserModel *model = self.listAry[indexPath.row];
    vc.conversationType = ConversationType_GROUP;
    vc.targetId = model.encode_id;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
